/**
 * Created by Administrator on 2017-11-25
 */

define(function (require, exports, module) {
    'use strict';
    var amount=0;
    function calc(id) {
        if ( !($("#"+id).hasClass("hover"))) {
            $("#"+id).addClass("hover");
            $("#"+id).children().css("display", "block");
            $(".settle").css("background-color",'#ff6e3c');
            var price =$("#"+id).parent().parent().find("#unit").html();
            var count =$("#"+id).parent().parent().find("#quantity").html();
             amount +=parseInt(count)*parseFloat(price).toFixed(2);
             for(var i=0;i<shopArray.length;i++){
                 if(shopArray[i].id == id){
                     reservearray.push(shopArray[i]);
                 }
             }
            $("#amounts").html(amount);
            if($(".choose").length == $(".hover").length){
                $(".chooseall").addClass("hover");
                $(".chooseall").children().css("display", "block");
            }
        } else {
            if($(".check").length == $(".hover").length){
                $(".chooseall").removeClass("hover");
                $(".chooseall").children().css("display", "none");
            }
            $("#"+id).removeClass("hover");
            $("#"+id).children().css("display", "none");
            var price =$("#"+id).parent().parent().find("#unit").html();
            var count =$("#"+id).parent().parent().find("#quantity").html();
            amount -= parseFloat(parseInt(count)*parseFloat(price).toFixed(2)).toFixed(2);
            $("#amounts").html(amount);
            for(var i=0;i<shopArray.length;i++){
                if(shopArray[i].id == id){
                    reservearray.splice(i, 1);
                }
            }
        }
    }

    function chooseall() {
        if(!$('.chooseall').hasClass('hover')){
            $(".check").addClass("hover");
            $(".check").children().css("display", "block");
            $(".settle").css("background-color",'#ff6e3c');
            var amounts=0;
            for(var i=0;i<shopArray.length;i++){
                amounts += parseFloat((parseInt(shopArray[i].count)) *parseFloat(shopArray[i].price).toFixed(2)).toFixed(2);
            }
            reservearray = shopArray;
            $("#amounts").html(amounts);
        }else {
            $(".check").removeClass("hover");
            $(".check").children().css("display", "none");
            $("#amounts").html(0);
            reservearray = [];
        }
    }

    //结算购物车
    function settlecart() {
        $("#bar").css("display",'none');
        MAIN.comfirmOrder();
    }

    return {
        calc: calc,
        settlecart:settlecart,
        chooseall:chooseall
    }
})